<?php

class Home extends CI_Controller{
    function __construct() {
        parent::__construct(); 
        $this->load->model('pedido_model');
        $this->load->model('persona_model');
        $this->load->model('Usuario_model');
        $this->load->library('encrypt');
         require_once('reporte.php');
    }
    public function index(){
        $this->args['title'] = 'Home';
        $this->args['content'] = 'home';
        $this->loadView();
    }
    public function inicioSesion(){
        $login=$this->loggedIn();
        $args['errorMsg'] = '';
        $args['title'] = 'Home';
        $args['content'] = 'home/home';             
        $args['username'] = $login['username'];
        $this->load->view('view',$args);  
    }  
    
    public function miChart(){
//      
        $reporte=new Reporte();
        $fin=date('Y-m-d');
        $inicio=date('Y-m-d', strtotime('-4 month'));
        $intervalo='M';
        $session_data = $this->session->userdata['logged_in'];
        $user=$session_data['user_id'];//$this->input->post('adel');     
        $trabas=  $this->pedido_model->comboTrabajosAll($user);
        $trabajos=array();
        if(count($trabas)!=0){
            foreach($trabas as $trab){
            array_push($trabajos, $trab['value']);
        } 
            $reporte->generarTiempos($inicio, $fin, $intervalo, $trabajos);
        $reporte->generarUtil($inicio, $fin, $intervalo);
        $reporte->generarPedidos($inicio, $fin);
        $reporte->generarProducción($inicio, $fin);
        }
        else {
            $args['aviso'] = "<div class=\"alert alert-error\"><h4>¡Aviso!</h4>No cuenta on trabajos registrados.</div>";
        }
               
        
        $login=$this->loggedIn();
        $args['idUsuario'] = $user;
        $args['errorMsg'] = '';
        $args['title'] = 'Mi Negocio';
        $args['content'] = 'home/miCharts';             
        $args['username'] = $login['username'];
        $this->load->view('view',$args);  
    }
    public function graficogantt(){
        $session_data = $this->session->userdata['logged_in'];        
        $usuarioSesion=$session_data['user_id'];
        $this->load->model('produccion_model');
        $bloquesGantt=$this->produccion_model->obtenerbloque($usuarioSesion);
        $default= array(
            'title'      => 'Trabajos',
            'cellwidth'  => 25,
            'cellheight' => 35);
        $args['bloquesGantt'] = $bloquesGantt;
        $args['default'] = $default;
        $login=$this->loggedIn();
        $args['title'] = 'Mi Producción';
        $args['content'] = 'produccion/graficogantt';        
        //$session_data = $this->session->userdata['logged_in'];
        $args['username'] = $login['username'];
        $this->load->view('view',$args);
    }
     public function miPerfil(){
        $login=$this->loggedIn();
        $session_data = $this->session->userdata['logged_in'];
        $user=$session_data['user_id'];//$this->input->post('adel');  
        $per=$this->persona_model->buscarUsuario($user);
        $persona=$per[0];
        $args['tipPer']=$persona['tip'];
        $args['razon']=$persona['razon'];
        $args['ruc']=$persona['ruc'];
        $args['rublo']=$persona['rubl'];
        $args['direc']=$persona['direc'];
        $args['telef']=$persona['telf'];
        $args['gen']=$persona['gen'];
        $args['nom']=$persona['nom'];
        $args['apellido']=$persona['ape'];
        $args['dni']=$persona['dni'];
        $args['telfce']=$persona['telcel'];
        $args['email']=$persona['email'];
        $args['pass']=$this->encrypt->decode($persona['pass']);        
        $args['errorMsg'] = '';
        $args['title'] = 'Mis Datos';
        $args['content'] = 'home/miPerfil';             
        $args['username'] = $login['username'];
        $this->load->view('view',$args);  
    }
    public function actualizar(){
        $login=$this->loggedIn();
        $session_data = $this->session->userdata['logged_in'];
        $user=$session_data['user_id'];//$this->input->post('adel');
        $tipPer = $this->input->post('tipPer');
        $razon = $this->input->post('razon');
        $ruc = $this->input->post('ruc');
        $rublo = $this->input->post('rublo');
        $direc = $this->input->post('direc');
        $telef = $this->input->post('telef');
        $gen = $this->input->post('gener');
        $nom= $this->input->post('nombres');
        $apellido = $this->input->post('apellid');
        $dni = $this->input->post('dni');
        $telfce = $this->input->post('telefcEL');
        $email = $this->input->post('email');
        $pass= $this->input->post('pass');
      
        $persona=array('RazonSocial'=>$razon,'Direccion'=>$direc,'Telefono'=>$telef,'Rublo'=>$rublo,'Email'=>$email,
            'Nombres'=>$nom,'Apellidos'=>$apellido,
            'DNI'=>$dni,'TelfCel'=>$telfce,'Genero'=>$gen,'TipoDePersona'=>$tipPer,'RUC'=>$ruc);
        $usuario=array('Email'=>$email,'Password'=>$this->encrypt->encode($pass));        
        $this->persona_model->actualizarPer($persona,$usuario,$user);
        //se recarga la página
        $per=$this->persona_model->buscarUsuario($user);
        $persona=$per[0];
        $args['tipPer']=$persona['tip'];
        $args['razon']=$persona['razon'];
        $args['ruc']=$persona['ruc'];
        $args['rublo']=$persona['rubl'];
        $args['direc']=$persona['direc'];
        $args['telef']=$persona['telf'];
        $args['gen']=$persona['gen'];
        $args['nom']=$persona['nom'];
        $args['apellido']=$persona['ape'];
        $args['dni']=$persona['dni'];
        $args['telfce']=$persona['telcel'];
        $args['email']=$persona['email'];  
        $args['pass']=$this->encrypt->decode($persona['pass']);   
        $args['errorMsg'] = '';
        $args['title'] = 'Mis Datos';
        $args['confirm'] = 'perfil';
        $args['mensaje'] = 'Se guardaron los datos';
        $args['content'] = 'home/miPerfil';             
        $args['username'] = $login['username'];
        $this->load->view('view',$args);
    }
}

?>
